﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;
using DistributedServices.MainModule.DTO;
using Application.MainModule.CustomerCompaniesManagement;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public CustomerCompany GetCustomerCompanyByUserId(Guid userId)
        {
            try
            {
                ICustomerCompanyManagementService customerCompanyService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerCompanyManagementService>();

                return customerCompanyService.FindCustomerCompanyByMembershipUserId(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeCustomerCompany(CustomerCompany customerCompany)
        {
            try
            {
                ICustomerCompanyManagementService customerCompanyService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerCompanyManagementService>();
                customerCompanyService.ChangeCustomerCompany(customerCompany);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveCustomerCompany(CustomerCompany customerCompany)
        {
            try
            {
                ICustomerCompanyManagementService customerCompanyService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerCompanyManagementService>();
                customerCompanyService.RemoveCustomerCompany(customerCompany);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddCustomerCompany(CustomerCompany customerCompany)
        {
            try
            {
                ICustomerCompanyManagementService customerCompanyService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerCompanyManagementService>();
                customerCompanyService.AddCustomerCompany(customerCompany);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public List<CustomerCompany> GetPagedCustomerCompany(PagedCriteria pagedCriteria)
        {
            try
            {
                ICustomerCompanyManagementService customerCompanyService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerCompanyManagementService>();

                return customerCompanyService.FindPagedCustomerCompanies(pagedCriteria.PageIndex, pagedCriteria.PageCount);
            }
            catch (ArgumentException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
            catch (NullReferenceException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
